/**
 * https://leetcode.cn/problems/range-sum-of-bst/
 */
function rangeSumBST(root: TreeNode | null, low: number, high: number): number {
  let ans = 0;

  const preOrder = (node: TreeNode | null) => {
    if (node === null) return;
    const val = node.val;
    if (val >= low && val <= high) {
      ans += val;
    }
    preOrder(node.left);
    preOrder(node.right);
  };

  preOrder(root);

  return ans;
}
